define(function(require, exports){
	var $ = require("jquery");
	var pubjs   = require('pubjs');
	var view    = require('@base/view');
	var util  = require('util');
	var User = pubjs.getUser();
	//var Father = User.originId == User.current.CreateUserId;

	// 广告位监测-媒体盒子-广告位盒子
	var AdsBox = view.container.extend({
		init: function(config){
			config = pubjs.conf(config,{
				'isShowChannel': true,
				'default_hide': false,
				'view_model': {
					'isShowChannel': true,
					'isCost': [],
					'vmSelectCost': this.vmSelectCost,
					'view': {
						'costPrice': pubjs.checkRight('spot.systemDeduct') //&& Father
					},
					'showCommonSize': true,
					'eventShowCommonSize': this.eventShowCommonSize,
					'eventShowSize': this.eventShowSize,
					'width': '',
					'height': ''
				}
			});

			this.Super('init',arguments);
		}
		,afterBuild: function(){
			var self = this;
			var c = self.getConfig();
			var el = self.getDOM();
			var doms = self.$doms = {};

			if(c.default_hide){
				el.hide();
			}

			$([
				'<div class="M-formConGroup addName">',
					'<label class="group-label">'+LANG('广告位：')+'</label>',
					'<div class="group-content">',
						'<span class="M-formInput" pub-name="addName" pub-mod="@base/common/input.text"/>',
					'</div>',
				'</div>',

				'<div class="M-formConGroup">',
					'<label class="group-label">'+LANG('单价：')+'</label>',
					'<div class="group-content">',
						'<span pub-mod="@base/common/drop.drop" pub-name="priceOption"></span>',
						'<span class="M-formInput price ml5" pub-name="price" pub-mod="@base/common/input.text"/>',
					'</div>',
				'</div>',

				'<div class="M-formConGroup" ms-visible="view.costPrice">',
					'<label class="group-label"></label>',
					'<div class="group-content costPrice" ms-visible="view.costPrice">',
						'<label>',
							'<input type="checkbox" ms-click="vmSelectCost()" ms-duplex="isCost" ms-value="1"/>',
							'<span>'+LANG('系统扣费')+'</span>',
						'</label>',
					'</div>',
				'</div>',

				'<div class="M-formConGroup" ms-visible="view.costPrice && isCost.length">',
					'<label class="group-label">'+LANG('单价0：')+'</label>',
					'<div class="group-content">',
						'<span pub-mod="@base/common/drop.drop" pub-name="systemAdType"></span>',
						'<span class="M-formInput price ml5" pub-name="cost" pub-mod="@base/common/input.text"/>',
					'</div>',
				'</div>',

				'<div class="M-formConGroup">',
					'<label class="mb10 group-label">'+LANG('形式：')+'</label>',
					'<div class="group-content" pub-name="type" pub-mod="modules/radioGroup.base">',

					'</div>',
				'</div>',

				'<div class="M-formConGroup locationRadio">',
					'<label class="mb10 group-label">'+LANG('位置：')+'</label>',
					'<div class="group-content" pub-name="location" pub-mod="modules/radioGroup.base">',
					'</div>',
				'</div>',

				'<div class="M-formConGroup">',
					'<label class="mb10 group-label">'+LANG('尺寸：')+'</label>',
					'<div class="group-content">',
						'<div ms-visible="showCommonSize">',
							'<span pub-mod="@base/common/drop.drop" pub-name="size"></span>',
							'<span class="ml10" pub-name="sizeTip" pub-mod="@base/tip.desc"></span>',
							'<span class="hyperlink ml15" ms-click="eventShowSize">'+ LANG("自定义尺寸") + '</span>',
						'</div>',
						'<div class="sizeCon" ms-visible="!showCommonSize">',
							'<input ms-duplex-value="width" class="w80 mr15" placeholder="'+ LANG('宽') +'"/>',
							'X',
							'<input ms-duplex-value="height" class="w80 ml15" placeholder="'+ LANG('高') +'"/>',
							'<span class="hyperlink ml15" ms-click="eventShowCommonSize">'+ LANG("常规尺寸") + '</span>',
						'</div>',
					'</div>',
				'</div>',

				'<div ms-visible="isShowChannel" class="M-formConGroup">',
					'<label class="group-label">'+LANG('频道：')+'</label>',
					'<div class="group-content">',
						'<span class="M-formInput" pub-name="channelName" pub-mod="@base/common/input.text"/>',
					'</div>',
				'</div>',


				'<i class="close icon-close"></i>'

			].join('')).appendTo(el);

			//初始化时隐藏位置选项
			doms.locationRadio = self.find('.locationRadio').hide();

			// 绑定关闭事件
			if(!c.closeBtn){
				self.find('.close').hide();
			}
			else{
				self.uiBind(self.find('.close'), 'click', self.eventCloseBtn);
			}

			self.vm.setItem('isShowChannel', c.isShowChannel);

			// 形式与位置的关系
			self.$typeOptions = [
				{'name':LANG('固定'),'id':'1'},
				{'name':LANG('富媒体'),'id':'2', 'location':
					[
						{'name': LANG('左下角'), 'id': '20001'},
						{'name': LANG('右下角'), 'id': '20002'}
					]
				},
				{'name':LANG('对联'),'id':'3'},
				{'name':LANG('视频'),'id':'4', 'location':
					[
						{'name': LANG('前贴'), 'id': '40001'},
						{'name': LANG('中插'), 'id': '40002'},
						{'name': LANG('暂停'), 'id': '40003'},
						{'name': LANG('后贴'), 'id': '40004'}
					]
				},
				{'name':LANG('弹窗'),'id':'5'}
			];

			// 尺寸表
			self.$sizeOptions = [
				{"name":"960*90", "id":"960*90", "val":5},
				{"name":"960*60", "id":"960*60", "val":4},
				{"name":"760*90", "id":"760*90", "val":8},
				{"name":"728*90", "id":"728*90", "val":7},
				{"name":"640*60", "id":"640*60", "val":3},
				{"name":"580*90", "id":"580*90", "val":6},
				{"name":"468*60", "id":"468*60", "val":2},
				{"name":"460*60", "id":"460*60", "val":1},
				{"name":"400*300", "id":"400*300", "val":17},
				{"name":"360*300", "id":"360*300", "val":16},
				{"name":"336*280", "id":"336*280", "val":15},
				{"name":"300*250", "id":"300*250", "val":13},
				{"name":"250*250", "id":"250*250", "val":12},
				{"name":"250*200", "id":"250*200", "val":10},
				{"name":"200*200", "id":"200*200", "val":11},
				{"name":"160*600", "id":"160*600", "val":19},
				{"name":"120*600", "id":"120*600", "val":18},
				{"name":"120*270", "id":"120*270", "val":14},
				{"name":"120*120", "id":"120*120", "val":9}
			]

			// 价格表
			self.$priceOptions = [
				{'id':'1','name':'CPM'},
				{'id':'2','name':'CPC'},
				{'id':'3','name':'CPD'},
				{'id':'4','name':'CPA'},
				{'id':'5','name':'CPS'},
				{'id':'6','name':'CPV'}
			]


			var config = {
				'addName': {
					'width': '80%'
				},
				'priceOption': {
					'value': 1,
					'width': 100,
					'panel_config': {
						//价格类型
						'options': self.$priceOptions,
						'hasSearch': false
					}
				},
				'systemAdType': {
					'value': 1,
					'width': 100,
					'panel_config': {
						//价格类型
						'options': self.$priceOptions,
						'hasSearch': false
					}
				},
				'price': {
					'width': 70,
					'placeholder': LANG("￥"),
					'afterText': LANG('元')
				},
				'cost': {
					'width': 70,
					'placeholder': LANG("￥"),
					'afterText': LANG('元')
				},
				'type':{
					'items': self.$typeOptions,
					'defaultValue': '1',
					'silent': false,
					'fireType': 'object',
					'width': 50
				},
				'location': {
					'silent': false
				},
				'size': {
					'type': 'button',
					'placeholder': LANG('请选择尺寸'),
					'value': '960*90',
					'width': 150,
					'panel_config': {
						//固定尺寸
						'options': self.$sizeOptions,
						'hasSearch': true,
						'height': 150,
						'appendTo': 'popup'
					}
				},
				'sizeTip': {
					'data': LANG('广告位尺寸，前面数字为宽度，后面数字为高度，以*号为间隔，例如‘960*90’。')
				},
				'channelName': {
					'width': '80%',
					'placeholder': LANG('默认频道'),
					'value': LANG('默认频道')
				}
			};
			self.buildFromTemplate(config);

		}
		,vmSelectCost: function() {
			this.$.priceOption.enable();
			this.$.price.enable();
			if(this.vm.$.isCost.length) {
				this.$.priceOption.disable();
				this.$.price.disable();
			}
		}
		,afterBuildTemplate: function(){
			var self = this;
			self.uiBind(self.$.channelName.find('input'), 'blur', self.eventChannelName);
			self.vmSelectCost();

			self.vm.watch('width', self.filterAddName);
			self.vm.watch('height', self.filterAddName);

		},
		onRadioGroupClick: function(ev){
			var self = this;
			switch (ev.name){
				case'type':
					self.changeLocationRadio(ev.param.id);
					break;
				case'location':
					self.filterAddName();
					break;
			}
			return false;
		},
		// 处理尺寸的失焦事件
		onDropBlur: function(ev){
			var self = this;
			switch (ev.name){
				case'size':
					self.filterAddName();
					break;

			}
			return false;
		},
		// 处理尺寸的改变事件
		onDropChange: function (ev){
			var self = this;
			switch (ev.name){
				case'size':
					self.filterAddName();
					break;

			}
			return false;
		},
		//设置广告位输入框默认值
		filterAddName: function(){
			var self = this;
			var c = self.getConfig();

			// 处理媒体名
			var mediaName = ''; // 媒体名
			if(c.isShowChannel){
				if(self.parent().$.media.getValue()){
					mediaName = self.parent().$.media.getValue().name;
				}
			}

			// 处理频道名
			var channelName = self.$.channelName.getValue(); // 频道名


			// 处理形式名
			var type = self.$.type.getValue(); // 形式值
			var location = self.$.location.getValue(); // 位置值
			var typeName = '';
			var typeData = util.find(self.$typeOptions, type, 'id');
			var locationData;

			if(typeData && typeData.location){
				locationData = util.find(typeData.location, location, 'id');
			}
			switch (+type){
				case 2:
				case 4:
					typeName = locationData && locationData.name;
					break;
				default:
					typeName = typeData && typeData.name;
			}

			// 处理尺寸名
			var sizeName = ''; // 尺寸名
			if (this.vm.$.showCommonSize)
			{
				if(self.$.size.getValue()){
					sizeName = self.$.size.getValue().name;
				}
			}
			else
			{
				sizeName = this.vm.$.width + '*' + this.vm.$.height;
			}


			var Name = [
				mediaName || LANG('默认媒体'),
				channelName,
				typeName,
				sizeName
			].join('_');
			self.$.addName.setValue(Name);
		},
		//设置广告位位置
		changeLocationRadio: function(id){
			var self = this;
			var location = self.$.location;
			var data = util.find(self.$typeOptions, id, 'id');
			switch (+id){
				case 2:
				case 4:
					self.$doms.locationRadio.show();
					location.setData(data && data.location);
					location.setValue(data && data.location && data.location[0].id);
					break;
				default:
					self.$doms.locationRadio.hide();
			}
			self.filterAddName();
		},
		eventChannelName: function(ev){
			this.filterAddName();
			return false;
		},
		eventCloseBtn: function(ev){
			var self = this;
			//清除模块
			self.fire('adsCloseBtn',ev);
			self.destroy(true);
			return false;
		},
		// 提取数据返回object
		getData: function(){
			var self = this;
			var vm = self.vm.$;
			var size = [];
			if(self.$.size.getValue(true).name){
				size = self.$.size.getValue(true).name.split('*');
			}
			var data = {};

			data.Name = self.$.addName.getValue();
			data.AdType = +self.$.priceOption.getValue(true).id;
			data.Price = +self.$.price.getValue() || 0;
			data.ShowType = +self.$.type.getValue();
			data.ShowSubType = self.$.location.getValue() || 0;

			// width*10000 + height
			var Width = +size[0];
			var Height = +size[1];
			// 输入自定广告位尺寸
			if (!vm.showCommonSize)
			{
				Width = +vm.width || 0;
				Height = +vm.height || 0;
			}
			data.Size = Width*10000+Height;
			data.MassChannelName = self.$.channelName.getValue();

			vm = self.vm.$;
			data.SystemDeduct = !!vm.isCost.length;
			if(data.SystemDeduct) {
				data.SystemAdType = +self.$.systemAdType.getValue(true).id;
				data.Cost = self.$.cost.getValue();
			}

			return data;
		},
		setData: function(data){
			var self = this;

			self.$.priceOption.setValue(data.AdType);
			if(data.SystemAdType) {
				self.$.systemAdType.setValue(data.SystemAdType);
			}
			self.$.price.setValue(String(data.Price));
			self.$.cost.setValue(String(data.Cost));
			self.$.type.setValue(data.ShowType);
			self.$.location.setValue(data.ShowSubType);
			if(data.ShowSubType){
				self.changeLocationRadio(+data.ShowType);
				self.$.location.setValue(data.ShowSubType);
			}
			//转化尺寸格式
			var size = self.renderPixelSize(data);
			if(!util.find(self.$.size.getData(), size, 'id')){
				// 没有的情况，新增一个尺寸
				self.$.size.addItem({'name':size,'id':size}).setValue(size);
			}else{
				self.$.size.setValue(size);
			}

			self.$.channelName.setValue(data.MassChannelName);
			self.$.addName.setValue(data.Name);
			var vm = self.vm.$;
			if(data.hasOwnProperty('SystemDeduct')) {
				if(data.SystemDeduct > 0) {
					vm.isCost = ['1'];
				}
				else {
					vm.isCost = [];
				}
			}

			self.vmSelectCost();
		},
		reset: function(){
			this.$.priceOption.reset();
			this.changeLocationRadio('1');
			this.$.type.setValue('1');
			this.$.price.setValue('');
			this.$.cost.setValue('');
			this.vm.reset();
		},
		/**
		 * 尺寸格式化
		 */
		renderPixelSize: function(data){
			if(data.Size) {
				return Math.floor(data.Size/10000) + '*' + (data.Size % 10000);
			}
			else {
				return '-';
			}
		},
		eventShowCommonSize: function()
		{
			this.vm.$.showCommonSize = true;
		},
		eventShowSize: function()
		{
			this.vm.$.showCommonSize = false;
		}
	});
	exports.main = AdsBox;
});